<?php

namespace app\admin\controller;

use app\helpers\Common;

use support\Request;
use think\facade\Db;
use think\Exception;

class ArticleController extends BaseController
{

    public function initialize()
    {
        parent::initialize();

        $this->defaultWhere = [
            ['status', '<>', 90]
        ];
        $this->defaultOrder = 'created_at DESC';
    }

    /**
     * Delete
     */
    public function delete(Request $request)
    {
        $className = '\\app\\' . $this->appName . '\\model\\' . $this->model;
        $model = new $className;

        $id = $request->input('id');
        $primaryKey = $this->primaryKey;
        if (isset($_POST['ids'])) {
            // 删除多条
            $ids = $request->input('ids');
            $result = $model->whereIn($primaryKey, $ids)->update(['status' => 90, 'deleted_id' => $this->admin['id'], 'deleted_at' => time()]);
        } else if ($id) {
            // 删除一条
            $info = $model->find($id);
            if (!$info) {
                return $this->error('数据不存在!');
            }
            $result = $model->where($primaryKey, $id)->update(['status' => 90, 'deleted_id' => $this->admin['id'], 'deleted_at' => time()]);
        }

        if ($result !== false) {
            // 写入日志
            $logInfo = $this->admin['name'] . '删除了一条' . __($this->model) . '数据。';
            Common::adminLog($request, $logInfo);

            return $this->success('删除成功!', url('/' . $this->model . '/index'));
        } else {
            return $this->error('删除失败!', url('/' . $this->model . '/index'));
        }
    }

    /**
     * 回收站
     */
    public function recyclebin(Request $request)
    {
        $this->indexView = 'recyclebin';
        $this->defaultWhere = [
            ['status', '=', 90]
        ];
        $this->defaultOrder = 'created_at DESC';

        return parent::index($request);
    }

    /**
     * 还原
     */
    public function restore(Request $request)
    {
        $className = '\\app\\' . $this->appName . '\\model\\' . $this->model;
        $model = new $className;

        $id = $request->input('id');
        $result = $model->where('id', $id)->update(['status' => 1, 'deleted_id' => 0, 'deleted_at' => 0]);

        if ($result !== false) {
            // 写入日志
            $logInfo = $this->admin['name'] . '还原了一条' . __($this->model) . '数据。';
            Common::adminLog($request, $logInfo);

            return $this->success('还原成功!', url('/' . $this->model . '/index'));
        } else {
            return $this->error('还原失败!', url('/' . $this->model . '/index'));
        }
    }

    /**
     * 彻底删除
     */
    public function clean(Request $request)
    {
        $className = '\\app\\' . $this->appName . '\\model\\' . $this->model;
        $model = new $className;

        $id = $request->input('id');
        $result = $model->where('id', $id)->delete();

        if ($result !== false) {
            // 写入日志
            $logInfo = $this->admin['name'] . '彻底删除了一条' . __($this->model) . '数据。';
            Common::adminLog($request, $logInfo);

            return $this->success('彻底删除成功!', url('/' . $this->model . '/index'));
        } else {
            return $this->error('彻底删除失败!', url('/' . $this->model . '/index'));
        }
    }

    /**
     * 筛选条件
     */
    public function getFilterWhere($request)
    {
        $param = $request->all();
        $where = [];
        if (isset($param['title']) && $param['title']) {
            $where[] = ['title', 'like', '%' . trim($param['title']) . '%'];
        }
        if (isset($param['range_date']) && $param['range_date']) {
            $rangeDate = explode(' ~ ', $param['range_date']);
            $startTime = strtotime($rangeDate[0]);
            $endTime = strtotime($rangeDate[1]);
            $where[] = ['created_at', '>=', $startTime];
            $where[] = ['created_at', '<=', $endTime];
        }

        return $where;
    }
}